﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

using FCNS.KeepMoney.Util;
using FCNS.KeepMoneyLibrary.Data;

namespace FCNS.KeepMoney.Data
{
    class DataFormatGDB:IDataFormat
    {
        public string Name
        {
            get { return "广发银行信用卡 CSV 格式"; }
        }

        public string Extension
        {
            get { return "(*.csv)|*.csv"; }
        }

        public string ExampleUrl
        {
            get
            {
                return "http://www.keepmoney.org/download/example/gdb.rar";
            }
        }

        public List<EntryField> ImportFile(string file)
        {
            List<EntryField> fields = new List<EntryField>();
            if (!File.Exists(file))
                return fields;

            int bankId = BankField.GetBankField.Find(bf => { return bf.Default; }).Id;
            int iGroupId = GroupField.GetDefaultGroup(true).Id;
            int eGroupId = GroupField.GetDefaultGroup(false).Id;

            try
            {
                StreamReader reader = new StreamReader(file, Encoding.Default);
                string line = reader.ReadLine();
                line = reader.ReadLine();//头部
                while (line != null)
                {
                    EntryField field ;

                    string[] str = line.Split(',');
                    string money = str[3];
                    if (!string.IsNullOrEmpty(money.Replace("\"", "").Trim()))//负
                    {
                        field = new EntryField(eGroupId);
                        if (money.EndsWith("\""))
                            money = money.Replace("\"", "").Trim();
                        else
                            money = (str[3] + str[4]).Replace("\"", "").Trim();
                    }
                    else
                    {
                        field = new EntryField(iGroupId);
                        money = str[4];
                        if (money.EndsWith("\""))
                            money = money.Replace("\"", "").Trim();
                        else
                            money = (str[4] + str[5]).Replace("\"", "").Trim();
                    }

                    field.Money = decimal.Parse(money);
                    field.Date = DateTime.Parse(str[0], null);
                    field.columnName = str[2];
                    field.columnNote = str[str.Length - 1];
                    field.BankId = bankId;

                    fields.Add(field);
                    line = reader.ReadLine();
                }
                reader.Dispose();
            }
            catch { System.Windows.Forms.MessageBox.Show("文件格式异常"); }

            return fields;
        }

        public void ExportFile(List<EntryField> fields, string file)
        {
            MessageBox.Show(LanguageUtil.功能不支持);
            //StreamWriter writer = new StreamWriter(file, false, Encoding.Default);
            //StringBuilder sb = new StringBuilder();//头部
            //    //case UiType.Gtd:
            //        //writer.WriteLine(Program.Translation.ApplyTo("开始日期,结束日期,任务,优先级,备注,进度"));

            //        //foreach (GtdTask task in gtdView.Tasks)
            //        //{
            //        //    sb.Append(task.StartTime.ToString(configApp.DateFormat) + ",");
            //        //    sb.Append(task.EndTime.ToString(configApp.DateFormat) + ",");
            //        //    sb.Append(task.Title + ",");
            //        //    sb.Append(task.Level + ",");
            //        //    sb.Append(task.Note + ",");
            //        //    sb.Append(task.Finish ? Program.Translation.ApplyTo("完成") : Program.Translation.ApplyTo("未完成"));

            //        //    writer.WriteLine(sb.ToString());
            //        //    sb.Remove(0, sb.Length);
            //        //}
            //        foreach (OLVColumn column in listViewEntry.Columns)
            //            sb.Append(column.Text + ",");

            //        sb.Remove(sb.Length - 1, 1);
            //        writer.WriteLine(sb.ToString());
            //        sb.Remove(0, sb.Length);
            //        //内容
            //        foreach (OLVListItem item in listViewEntry.Items)
            //        {
            //            foreach (OLVListSubItem sub in item.SubItems)
            //                sb.Append(sub.Text + ",");

            //            sb.Remove(sb.Length - 1, 1);
            //            writer.WriteLine(sb.ToString());
            //            sb.Remove(0, sb.Length);
            //        }

            //writer.Flush();
            //writer.Dispose();
        }
    }
}